<?php

namespace Database\Seeders;

use App\Models\CategoryModel;
use Illuminate\Database\Seeder;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $categoryList = [
            [
                'name' => '副职导航',
                'icon' => 'icon-avatar',
                "children" => [
                    '小游戏',
                    '搞笑段子'
                ]
            ],
            [
                'name' => '开发工具',
                'icon' => 'icon-avatar',
                "children" => [
                    '前端工具',
                    '后端工具'
                ]
            ]
        ];

        // 循环批量插入数据
        foreach ($categoryList as $categoryItem) {
            // 创建子数据
            $nowCategory = CategoryModel::query()->create([
                'name' => $categoryItem['name'],
                'icon' => $categoryItem['icon'],
                'status' => CategoryModel::STATUS_OPEN
            ]);

            // 插入子集数据
            $insertData = [];
            foreach ($categoryItem['children'] as $childrenItem) {
                $insertData[] = [
                    'pid' => $nowCategory->id,
                    'name' => $childrenItem,
                    'status' => CategoryModel::STATUS_OPEN
                ];
            }
            if (!empty($insertData)) {
                CategoryModel::query()->insert($insertData);
            }
        }
    }
}
